// @flow
import React from 'react'
import styles from './AddDevicePoint.css'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { Button } from 'antd'
import DevicePoint from 'components/DevicePoint'

type Props = {
  history: Array<any>,
  location: Object
}
type States = {
  pointData: Array<Object>,
  partData: Array<Object>,
  devicePoint: Array<Object>
}
class AddDevicePoint extends React.Component {
  props: Props
  state: States
  getPointData: Function
  goBack: Function
  getPartsData: Function
  getDevicePoint: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      pointData: [],
      partData: [],
      devicePoint: []
    }
    this.getPointData = this.getPointData.bind(this)
    this.goBack = this.goBack.bind(this)
    this.getPartsData = this.getPartsData.bind(this)
    this.getDevicePoint = this.getDevicePoint.bind(this)
  }
  goBack () {
    history.go(-1)
  }
  componentWillMount () {
    this.getPointData()
    this.getPartsData()
    this.getDevicePoint()
  }
  getDevicePoint () {
    universalFetch(`${__SERVICE_API__}device/templates/monitors/points`)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({
          code: json.code,
          message: json.message
        }))
      }
      this.setState({
        devicePoint: json.result
      })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  getPointData () {
    const { data } = this.props.location.state
    universalFetch(`${__SERVICE_API__}device/templates/monitors/points?root_id=${data.id}`)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({
          code: json.code,
          message: json.message
        }))
      }
      this.setState({
        pointData: json.result
      })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  getPartsData () {
    const { data } = this.props.location.state
    universalFetch(`${__SERVICE_API__}device/templates/devices?id=${data.id}&type=wide`)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({
          code: json.code,
          message: json.message
        }))
      }
      this.setState({
        partData: json.result
      })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  render () {
    const { pointData, partData, devicePoint } = this.state
    const { data } = this.props.location.state
    return (
      <div className={styles['main']}>
        <section className={styles['header']}>
          <div className={styles['header-title']}>
            <span>名称: </span>
            <span> {data.name}
            </span>
          </div>
          <div className={styles['header-content']}>
            <span>类型: {data.type}</span>
            <span>型号: {data.spec}</span>
          </div>
          <div className={styles['setting']} title='设置'>
            <Button onClick={this.goBack} className={styles['settingBtn']} >返回</Button>
          </div>
        </section>
        <DevicePoint {...{
          location,
          pointData,
          partData,
          devicePoint,
          getPointData: this.getPointData,
          deviceId: data.id
        }} />
      </div>
    )
  }
}

export default AddDevicePoint
